<?
global $DB;
//Modül ayarları yükleniyor...
include_once "settings.inc.php";

if(isset($_POST['process']))
{
	switch ($_POST['process']) 
	{
		case 'changeStatus':
			$sql = "SELECT status FROM ". _MODULE_NAME ." WHERE id=" . $_POST['id'];
			$rows = $DB->fetchArray($sql);
			if(is_array($rows[0]))
			{
				if($rows[0]['status'] == 1)	//-> Durumu aktif ise pasif yap
				{
					if($DB->query("UPDATE ". _MODULE_NAME ." SET status=0 WHERE id=" . $_POST['id']))
						echo 'passive';
					else
						echo 'ERROR';
				}
				elseif($rows[0]['status'] == 0)	//-> Durumu pasif ise aktif yap
				{
					if($DB->query("UPDATE ". _MODULE_NAME ." SET status=1 WHERE id=" . $_POST['id']))
						echo 'active';
					else
						echo 'ERROR';
				}
			}
			else
				echo 'ERROR';
		break;

		case "deletePicture":
			$sql = "SELECT picture FROM ". _MODULE_NAME ." WHERE id=" . $_POST['id'];
			$rows = $DB->fetchArray($sql);
			if(is_array($rows[0]))
			{	
				if($DB->query("UPDATE " . _MODULE_NAME . " SET picture ='' WHERE id=" . $_POST['id']))
				{
					if(file_exists("../pictures/" . _MODULE_NAME . "/thumbs/" . $rows[0]['picture']) or file_exists("../pictures/" . _MODULE_NAME . "/bigs/" . $rows[0]['picture']))
					{
						@unlink("../pictures/" . _MODULE_NAME . "/thumbs/" . $rows[0]['picture']);
						@unlink("../pictures/" . _MODULE_NAME . "/bigs/" . $rows[0]['picture']);
					}
					echo 'OK';
				}
				else
					echo 'ERROR';
			}
			else
				echo 'ERROR1';
		break;

		case "sortRow":
			if(isset($_POST['startId']) and isset($_POST['stopId']) and isset($_POST['currentId']) and isset($_POST['direction'])){				
				
				$startRow = getRowById(_MODULE_NAME, $_POST['startId']);
				$stopRow = getRowById(_MODULE_NAME, $_POST['stopId']);
				$direction = $_POST['direction'];
				$currentId = $_POST['currentId'];
				
				if($direction == "up")
				{
					//Yukarı taşınmış row -
					$idList = $DB->fetchArray("SELECT id FROM " . _MODULE_NAME . " WHERE row>=".$startRow." and row<=".$stopRow);
					if(is_array($idList[0]))
					{
						$ids = "";
						foreach($idList as $idx){
							if($ids == "")
								$ids .= "id='".$idx["id"]."'";
							else
								$ids .= "or id='".$idx["id"]."'";
						}
						
						$sql1 = "UPDATE " . _MODULE_NAME . " SET row=row-10 WHERE " . $ids;
						$sql2 = "UPDATE " . _MODULE_NAME . " SET row=" . ($stopRow) . " WHERE id='".$currentId."'";
						
						echo $sql1."\nSELECT id FROM " . _MODULE_NAME . " WHERE row>=".$startRow." and row<=".$stopRow; 
						
						if($DB->query($sql1) and $DB->query($sql2))
						{
							echo 'OK';
							exit;
						}
						else
						{
							echo 'ERROR2';
							exit;
						}
					}
					else
					{
						echo 'ERROR11'."\nSELECT id FROM " . _MODULE_NAME . " WHERE row>=".$startRow." and row<=".$stopRow;
						exit;
					}
				}
				else if($direction == "down")
				{
					//Aşağı taşınmış row +
					$idList = $DB->fetchArray("SELECT id FROM " . _MODULE_NAME . " WHERE row<=".$startRow." and row>".$stopRow);
					if(is_array($idList[0]))
					{
						$ids = "";
						foreach($idList as $idx){
							if($ids == "")
								$ids .= "id='".$idx["id"]."'";
							else
								$ids .= "or id='".$idx["id"]."'";
						}
						
						$sql1 = "UPDATE " . _MODULE_NAME . " SET row=row+10 WHERE " . $ids;
						$sql2 = "UPDATE " . _MODULE_NAME . " SET row=" . ($stopRow) . " WHERE id='".$currentId."'";
						
						if($DB->query($sql1) and $DB->query($sql2))
						{
							echo 'OK';							
							exit;
						}
						else
						{
							echo 'ERROR2';
							exit;
						}
					}
					else
					{
						echo 'ERROR12';
						exit;
					}
				}
				else
					echo 'ERRORx1';
			}
		break;
		
		case "changeRow":
			if(isset($_POST['id']) and isset($_POST['newRow']))
			{
				$id = intVal($_POST['id']);
				$newRow = intVal($_POST['newRow']);
				if($id != "" and $newRow != "")
				{
					$sql = "UPDATE " . _MODULE_NAME . " SET row=" . $newRow . " WHERE id=" . $id;
					if($DB->query($sql))
					{
						echo 'OK';
						exit;
					}
					else
					{
						echo 'ERROR'; 
						exit;
					}
				}
				else
				{
					echo 'NONE';
					exit;
				}
			}
		break;
		
		default:
			echo 'ERRORx2';
		break;
	}
}
else
	echo 'ERRORx';
?>